package com.forum.module.system.controller.admin.role;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.module.system.controller.admin.role.vo.RolePageReqVO;
import com.forum.module.system.controller.admin.role.vo.RoleRespVO;
import com.forum.module.system.controller.admin.role.vo.RoleSaveReqVO;
import com.forum.module.system.convert.role.RoleCovert;
import com.forum.module.system.dal.dataobject.role.RoleDO;
import com.forum.module.system.service.role.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/15 20:50
 */
@RestController
@RequestMapping("/system/role")
@Tag(name = "管理后台 -- 角色")
public class RoleController {

    private final RoleService roleService;


    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增角色")
    public DataResult<Long> createRole(@RequestBody RoleSaveReqVO reqVO) {
        Long id = roleService.createRole(reqVO);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色信息")
    public DataResult<Boolean> updateRole(@RequestBody RoleSaveReqVO reqVO) {
        roleService.updateRole(reqVO);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询角色列表")
    public DataResult<PageResult<RoleRespVO>> page(@RequestBody @Valid RolePageReqVO pageReqVO) {
        // 获得角色分页列表
        PageResult<RoleDO> pageResult = roleService.getRolePage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

        return success(new PageResult<>(RoleCovert.INSTANCE.covertList(pageResult.getList()), pageResult.getTotal()));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色信息")
    @Parameter(name = "id", description = "角色编号", required = true, example = "1024")
    public DataResult<Boolean> deleteRole(@RequestParam("id") Long id) {
        roleService.deleteById(id);
        return success(true);
    }


}
